<template>
  <div class="manage">
    <el-dialog
      :visible.sync="isShow"
      :title="operateType === 'add' ? '新增商品' : '更新商品'"
    >
      <CommonForm :formLabel="operateFromLabel" :form="operateFrom">
      </CommonForm>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm = false">确 定</el-button>
      </div>
    </el-dialog>
    <div class="header">
      <el-button type="primary" icon="el-icon-circle-plus"> 新增</el-button>
      <CommonForm inline :formLabel="formLabel" :form="searchForm">
        <el-button type="primary" icon="el-icon-search"> 搜索</el-button>
      </CommonForm>
    </div>
    <CommonTable
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @current-change="getList"
      @edit="editUser"
      @del="delUser"
    ></CommonTable>
  </div>
</template>

<script>
import CommonForm from "@/layout/CommonForm.vue";
import CommonTable from "@/layout/CommonTable.vue";
export default {
  components: {
    CommonForm,
    CommonTable
  },
  data() {
    return {
      isShow: false,
      operateType: "add",
      operateFrom: {
        category: "",
        name: "",
        price: "",
        sales: "",
        remanin: "",
      },
      operateFromLabel: [
        {
          model: "category",
          label: "商品类别"
        },
        {
          model: "name",
          label: "商品名"
        },
        {
          model: "price",
          label: "单价",
        },
        {
          model: "sales",
          label: "销售量"
        },
		{
		  model: "remanin",
		  label: "存货"
		}
      ],
      searchForm: {
        keyword: ""
      },
      formLabel: [
        {
          model: "keyword",
          label: ""
        }
      ],
      tableData: [],
      tableLabel: [
        {
          model: "category",
          label: "商品类别"
        },
        {
          model: "name",
          label: "商品名"
        },
        {
          model: "price",
          label: "单价",
		  width: 250
        },
        {
          model: "sales",
          label: "销售量",
		  width: 250
        },
        {
          model: "remanin",
          label: "存货",
		  width: 250
        }
      ],
      config: {
        page: 1,
        total: 30,
        loading: false
      }
    };
  },
  methods: {
    //无误获取参数
    getList() {
      this.config.loading = true;
      this.$http
        .get("http://localhost:9527/user/get", { page: this.config.page })
        .then(res => {
          console.log(res.data); //调试看数据是否获取到
          this.tableData = res.data.list.map(item => {
            item.sexLabel = item.sex === 0 ? "女" : "男";
            return item;
          });
          this.config.total = res.data.count;
          this.config.loading = false;
        });
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      console.log(row);
      this.operateFrom = row;
    },
    confirm() {
      if (this.operateType === "edit") {
        this.$http
          .get("http://localhost:9527/user/update", this.operateFrom)
          .then(res => {
            console.log(res.data);
            this.isShow = false;
            this.getList(); //编辑成功后需要重新刷新用户信息
          });
      }
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let id = row.id;
          this.$http
            .get("http://localhost:9527/user/delete", {
              params: {
                id
              }
            })
            .then(res => {
              console.log(res.data);
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },

  created() {
    //创建将数据请求发出
    this.getList();
    // this.$http
    //   .get("http://localhost:9527/user/get", this.config)
    //   .then(res => {
    //     console.log(res.data);
    //   })
    //   .catch(err => {
    //     console.log(err);
    //   });
  },
  name: "User"
};
</script>

<style lang="scss" scoped>
@import "~@/styles/manage.scss";
</style>
